package com.data.migration.service;

import com.data.migration.entity.Operation;
import com.data.migration.util.DBUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Peter on 2018/12/8.
 * 迁移1v1课程数据
 */
public class Step_7_Appointment {
    public static final String step = "7";
    public static final String stepDesc = "约课相关数据";

    private static final Logger logger = LoggerFactory.getLogger(Step_7_Appointment.class);
    public List<Operation> insertSql = new ArrayList<>();//插入课程的sql

    public static void main(String[] args) throws SQLException {
        Step_7_Appointment step = new Step_7_Appointment();
        step.start();
//        step.clean();

    }

    public void start() throws SQLException {
        logger.info("\n******************************************************************************" +
                "\n======================== Step 7: 约课相关数据 start ================================" +
                "\n******************************************************************************\n");
        step_1();
        step_1_2();
        step_1_3();
        step_1_4();
        step_1_5();

        logger.info("\n******************************************************************************" +
                "\n======================== Step 7: 约课相关数据 end ================================" +
                "\n******************************************************************************\n");
    }

    public void clean() throws SQLException {
        logger.info("\n******************************************************************************" +
                "\n======================== 【clean】 Step 7: 约课相关数据 start =========================" +
                "\n******************************************************************************");
        // 1. 学生上课PPT页数记录【td_nt_memb_course】
        step_1_clean();
        logger.info("\n******************************************************************************" +
                "\n======================== 【clean】 Step 7: 约课相关数据 end =========================" +
                "\n******************************************************************************");
    }


    // 1. 约课记录【td_nt_reserve】    ------------------------------------------------------------------------------1
    public void step_1() throws SQLException {
        logger.info("\n     --------- 7.1 约课记录【td_nt_reserve】 start --------- ");
        Connection conn = DBUtil.getConn(Operation.app_type_T);
        String tableName = "td_nt_reserve"; // 表名
        String pk = "reserve_id"; // 主键（自增）的列名字
        insertSql = DBUtil.buildInsertSql(conn, tableName, pk, step, stepDesc);
        DBUtil.insertBatch(insertSql);
    }

    // 更新外键【td_nt_reserve.memb_id】
    public void step_1_2() throws SQLException {
        String ftableName = "td_nt_reserve"; // 附表表名
        String fk = "memb_id";// 附表外键
        String ptableName = "td_nt_member";//主表的表名
        String pk = "MEMB_ID"; // 主表的主键
        String sql = DBUtil.createUpdateFkSql(ftableName, fk, ptableName, pk);
        Connection conn = DBUtil.getConn(Operation.app_type_A);
        DBUtil.executeUpdate(conn, sql);
    }

    // 更新外键【td_nt_reserve.tea_id】
    public void step_1_3() throws SQLException {
        String ftableName = "td_nt_reserve"; // 附表表名
        String fk = "tea_id";// 附表外键
        String ptableName = "td_nt_teacher";//主表的表名
        String pk = "TEA_ID"; // 主表的主键
        String sql = DBUtil.createUpdateFkSql(ftableName, fk, ptableName, pk);
        Connection conn = DBUtil.getConn(Operation.app_type_A);
        DBUtil.executeUpdate(conn, sql);
    }

    // 更新外键【td_nt_reserve.cou_id】
    public void step_1_4() throws SQLException {
        String ftableName = "td_nt_reserve"; // 附表表名
        String fk = "cou_id";// 附表外键
        String ptableName = "ts_nt_course_info";//主表的表名
        String pk = "cou_ID"; // 主表的主键
        String sql = DBUtil.createUpdateFkSql(ftableName, fk, ptableName, pk);
        Connection conn = DBUtil.getConn(Operation.app_type_A);
        DBUtil.executeUpdate(conn, sql);
    }
    // 更新外键【td_nt_reserve.memb_set_id】
    public void step_1_5() throws SQLException {
        String ftableName = "td_nt_reserve"; // 附表表名
        String fk = "memb_set_id";// 附表外键
        String ptableName = "td_nt_memb_setmeal";//主表的表名
        String pk = "memb_set_id"; // 主表的主键
        String sql = DBUtil.createUpdateFkSql(ftableName, fk, ptableName, pk);
        Connection conn = DBUtil.getConn(Operation.app_type_A);
        DBUtil.executeUpdate(conn, sql);
    }

    public void step_1_clean() throws SQLException {
        logger.info("\n     ---------【clean】7.1 约课记录【td_nt_reserve】 start --------- ");
        String sql = "delete from td_nt_reserve where app_type='T'";
        Connection conn = DBUtil.getConn(Operation.app_type_A);
        DBUtil.executeUpdate(conn, sql);
    }

}
